package com.yhyvr.api.service.store;/**
 * Created by zhangqin on 2017/5/25.
 */

import com.google.common.collect.Lists;
import com.lz.mybatis.jdbc.auto.dao.BoBuyCartMapper;
import com.lz.mybatis.jdbc.auto.dao.BoGoodsMapper;
import com.lz.mybatis.jdbc.auto.model.BoBuyCart;
import com.lz.mybatis.jdbc.auto.model.BoBuyCartExample;
import com.lz.mybatis.jdbc.auto.model.BoGoods;
import com.lz.tool.ListUtils;
import com.lz.tool.MathUtil;
import com.lz.tool.UUIDTool;
import com.yhyvr.api.handle.AuthAnnotation;
import com.yhyvr.pojo.req.api.ApiReqBody;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.store.buycard.ApiStoreBuyCardAddReq;
import com.yhyvr.pojo.req.api.store.buycard.ApiStoreBuyCardDeleteReq;
import com.yhyvr.pojo.req.api.store.buycard.ApiStoreBuyCardSaveReq;
import com.yhyvr.pojo.resp.api.ApiRespBody;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.store.buycard.ApiStoreBuyCardListResp;
import com.yhyvr.service.mapper.CommonInfoServiceMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 购物车服务
 *
 * @author
 * @create 2017-05-25 15:44
 **/

@Service
public class ApiBuyCardService {

    @Resource
    private CommonInfoServiceMapper commonInfoServiceMapper;
    @Resource
    private BoBuyCartMapper boBuyCartMapper;
    @Resource
    private BoGoodsMapper boGoodsMapper;
    @AuthAnnotation(iLogin = true)
    public ApiRespData<List<ApiStoreBuyCardListResp>> listCard(ApiReqData<ApiReqBody> apiReqBodyApiReqData){
        String uid = apiReqBodyApiReqData.getCookieUserId();
        BoBuyCartExample boBuyCartExample = new BoBuyCartExample();
        boBuyCartExample.createCriteria().andUserIdEqualTo(uid);
        List<BoBuyCart> boBuyCarts = boBuyCartMapper.selectByExample(boBuyCartExample);
        List<ApiStoreBuyCardListResp> apiStoreBuyCardListResps = Lists.newArrayList();
        if(ListUtils.isNotBlank(boBuyCarts)){
            for (BoBuyCart boBuyCart : boBuyCarts) {
                ApiStoreBuyCardListResp apiStoreBuyCardListResp = new ApiStoreBuyCardListResp();
                apiStoreBuyCardListResp.setCardId(boBuyCart.getId());
                apiStoreBuyCardListResp.setGoodsId(boBuyCart.getGoodsId());
                apiStoreBuyCardListResp.setGoodsNumber(boBuyCart.getGoodsNumber());
                BoGoods boGoods = boGoodsMapper.selectByPrimaryKey(boBuyCart.getGoodsId());
                if(boGoods!=null){
                    apiStoreBuyCardListResp.setGoodsImage(boGoods.getGoodsImage());
                    apiStoreBuyCardListResp.setGoodsTitle(boGoods.getGoodsTitle());
                    apiStoreBuyCardListResp.setGoodsPrice(MathUtil.getTwoROUNDtoString(boGoods.getGoodsPrice()));
                }
                apiStoreBuyCardListResps.add(apiStoreBuyCardListResp);
            }
        }
        return ApiRespData.buildSucc(apiStoreBuyCardListResps);
    }

    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiRespBody> deleteBuyCard(ApiReqData<ApiStoreBuyCardDeleteReq> apiStoreBuyCardDeleteReqApiReqData){
        String uid = apiStoreBuyCardDeleteReqApiReqData.getCookieUserId();
        String[] ids = apiStoreBuyCardDeleteReqApiReqData.getBody().getCardIds().split(",");
        List<String> list = Lists.newArrayList(ids);
        BoBuyCartExample boBuyCartExample = new BoBuyCartExample();
        boBuyCartExample.createCriteria().andUserIdEqualTo(uid).andIdIn(list);
        boBuyCartMapper.deleteByExample(boBuyCartExample);
        return ApiRespData.buildSucc();
    }


    @AuthAnnotation(iLogin = true)
    public ApiRespData<Long> commonInfo(ApiReqData<ApiReqBody> commonReq){
        String uid = commonReq.getCookieUserId();
        return ApiRespData.buildSucc(commonInfoServiceMapper.countBuyCardGoodsNumber(uid));
    }

    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiRespBody> addBuyCard(ApiReqData<ApiStoreBuyCardAddReq> apiStoreBuyCardAddReqApiReqData){
        String uid = apiStoreBuyCardAddReqApiReqData.getCookieUserId();
        ApiStoreBuyCardAddReq body = apiStoreBuyCardAddReqApiReqData.getBody();
        String goodsId = body.getGoodsId();
        int goodsNumber = body.getGoodsNumber();
        BoBuyCartExample boBuyCartExample = new BoBuyCartExample();
        boBuyCartExample.createCriteria().andUserIdEqualTo(uid).andGoodsIdEqualTo(goodsId);
        BoBuyCart boBuyCart = ListUtils.getFirst(boBuyCartMapper.selectByExample(boBuyCartExample));
        if(boBuyCart!=null){
            boBuyCart.setGoodsNumber(goodsNumber+boBuyCart.getGoodsNumber());
            boBuyCartMapper.updateByPrimaryKey(boBuyCart);
        }else{
            boBuyCart = new BoBuyCart();
            boBuyCart.setId(UUIDTool.getUUID());
            boBuyCart.setGoodsNumber(goodsNumber);
            boBuyCart.setCreateTime(new Date());
            boBuyCart.setGoodsId(goodsId);
            boBuyCart.setUserId(uid);
            boBuyCartMapper.insertSelective(boBuyCart);
        }

        return ApiRespData.buildSucc();
    }
    @AuthAnnotation(iLogin = true)
    public ApiRespData<ApiRespBody> saveBuyCard(ApiReqData<ApiStoreBuyCardSaveReq> apiStoreBuyCardAddReqApiReqData){
        String uid = apiStoreBuyCardAddReqApiReqData.getCookieUserId();
        ApiStoreBuyCardSaveReq body = apiStoreBuyCardAddReqApiReqData.getBody();
        String goodsId = body.getGoodsId();
        int goodsNumber = body.getGoodsNumber();

        int goodsType = body.getGoodsType();
        BoBuyCartExample boBuyCartExample = new BoBuyCartExample();
        boBuyCartExample.createCriteria().andUserIdEqualTo(uid).andGoodsIdEqualTo(goodsId);
        BoBuyCart boBuyCart = ListUtils.getFirst(boBuyCartMapper.selectByExample(boBuyCartExample));
        if(boBuyCart!=null){
            if(goodsNumber==0){
                boBuyCartMapper.deleteByPrimaryKey(boBuyCart.getId());
            }else {
                boBuyCart.setGoodsNumber(goodsNumber);
                boBuyCartMapper.updateByPrimaryKey(boBuyCart);
            }
        }

        return ApiRespData.buildSucc();
    }
}
